package com.cloudcc.mobile;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.cloudcc.cloudframe.net.async.AsyncClient;
import com.cloudcc.cloudframe.util.SharePreferece;
import com.cloudcc.mobile.manager.CacheManager;
import com.cloudcc.mobile.manager.ContactsManager;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.presenter.LoginPresenter;
import com.cloudcc.mobile.service.BindingService;
import com.cloudcc.mobile.service.DingWeiService;
import com.cloudcc.mobile.util.NotificationUtils;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.view.login.LoginNewActivity;

/**
 * @Description: 应用程序Activity管理类：用于Activity管理和应用程序退出
 * @author wangzhiqiang
 * @date 2015-11-10 上午11:31:05
 * 
 */
public class AppManager {

	private static AppManager instance;

	private AppManager() {
	}

	public synchronized static AppManager getInstance() {
		if (instance == null) {
			instance = new AppManager();
		}
		return instance;
	}

	/**
	 * 获得activity 栈
	 * 
	 * @return
	 */
	public synchronized Stack<Activity> getActivityStack() {
		if (AppContext.activityStack == null) {
			AppContext.activityStack = new Stack<Activity>();
		}
		return AppContext.activityStack;
	}

	/**
	 * 添加Activity到堆栈
	 */
	public void push(Activity activity) {
		getActivityStack().push(activity);
	}

	/**
	 * 结束指定的Activity
	 */
	public void pop(Activity activity) {
		if (activity != null) {
			getActivityStack().remove(activity);
			activity = null;
		}
	}

	/**
	 * 获取当前Activity（堆栈中最后一个压入的）
	 */
	public Activity getCurrentActivity() {
		Activity activity = getActivityStack().lastElement();
		return activity;
	}

	/**
	 * 结束当前Activity（堆栈中最后一个压入的）
	 */
	public void finishCurrentActivity() {
		Activity activity = getActivityStack().lastElement();
		if (activity != null) {
			activity.finish();
			getActivityStack().remove(activity);
			activity = null;
		}
	}

	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls) {
		for (Activity activity : getActivityStack()) {
			if (activity.getClass().equals(cls)) {
				if (activity != null) {
					activity.finish();
					getActivityStack().remove(activity);
					activity = null;
				}
			}
		}
	}

	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity() {
		Stack<Activity> activityStack = getActivityStack();
		int size = activityStack.size();
		for (int i = 0; i < size; i++) {
			if (null != activityStack.get(i)) {
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();
	}

	/**
	 * 是否是在前台
	 * 
	 * @return
	 */
	public boolean hasForegroundActivies() {
		return !AppContext.activityStack.empty();
	}

	public void AppExitOld() {
		Context context = AppContext.getInstance();
		/**
		 * 清除通知
		 */
		NotificationUtils.clearNotification(context, 0);
		NotificationUtils.clearNotification(context, 1);
		NotificationUtils.clearNotification(context, 2);
		NotificationUtils.clearNotification(context, 3);
		/**
		 * 关闭定位上传
		 */
		AppContext.isStopLocation = true;
		Intent service = new Intent(context, DingWeiService.class);
		context.stopService(service);
		/**
		 * 关闭service获取binding
		 */
		AppContext.isStopService = true;
		Intent binding = new Intent(context, BindingService.class);
		context.stopService(binding);

	}

	public void AppExit() {
		try {
			for (Activity activity : getActivityStack()) {
				if (activity != null) {
					activity.finish();
				}
			}
		} catch (Exception e) {
			Tools.handle(e);
		}

		clearTestModel();

	}

	public void clearTestModel() {
		// 清空测试数据
		if (RunTimeManager.getInstance().isTestModel()) {
			LoginPresenter loginPresenter = new LoginPresenter();
			System.out.println("帐号:" + loginPresenter.getLastLogin().userName);
			loginPresenter.delLogin(loginPresenter.getLastLogin().userName);
			CacheManager.getInstance().clear();
			UserManager.getManager().clearUser();
			RunTimeManager.getInstance().clearUserCache();
			ContactsManager.getInstance().clearContactCache();
			AsyncClient.getInstance().clear(AppContext.getInstance());
		}
	}

	/**
	 * 退出应用程序
	 */
	public void AppExit(Context context) {
		try {
			finishAllActivity();
			ActivityManager activityMgr = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			activityMgr.restartPackage(context.getPackageName());
			System.exit(0);
		} catch (Exception e) {
		}
	}
}
